package fourFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.adapter.CollegeIndexVolunteerListAdapter;
import mainFragment.bean.VolunteerListBean;
import utils.MyLogUtils;
import view.MyPtrRefresh_LoadView;
import volunteer.activity.Volunteer;

/**
 * Created by Administrator on 2017/4/7.
 * 我关注的志愿者
 */
@SuppressLint("ValidFragment")
public class MyFocusVolunteer extends BaseFragment {
    private View view;
    private MyPtrRefresh_LoadView my_focus_volunteer_refreshLayout;
    private ListView my_focus_volunteer_SwipeMenuListView;



    private ArrayList<VolunteerListBean.VolunteerListbean> arrayList1;
    private CollegeIndexVolunteerListAdapter collegeIndexVolunteerListAdapter;

    private final int ONFRESH = 0;
    private final int ONLOAD = 1;
    private int page = 1;
    private static final String TAG = "MyFocusVolunteer";

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    switch (msg.arg1) {
                        case ONFRESH:
                            Gson gson = new Gson();
                            VolunteerListBean myFocusVolunteerBean = gson.fromJson((String) msg.obj, VolunteerListBean.class);
                            if (myFocusVolunteerBean != null) {
                                List<VolunteerListBean.VolunteerListbean> expertList = myFocusVolunteerBean.getVolunteerList();
                                if (expertList != null) {
                                    arrayList1.clear();
                                    if (expertList.size() > 0) {
                                        for (int i = 0; i < expertList.size(); i++) {
                                            arrayList1.add(expertList.get(i));
                                        }
                                    }
                                    collegeIndexVolunteerListAdapter.notifyDataSetChanged();
                                }
                            }
                            break;
                        case ONLOAD:
                            Gson gson1 = new Gson();
                            VolunteerListBean myFocusVolunteerBean1 = gson1.fromJson((String) msg.obj, VolunteerListBean.class);
                            if (myFocusVolunteerBean1 != null) {
                                List<VolunteerListBean.VolunteerListbean> expertList = myFocusVolunteerBean1.getVolunteerList();
                                if (expertList != null) {
                                    if (expertList.size() > 0) {
                                        for (int i = 0; i < expertList.size(); i++) {
                                            arrayList1.add(expertList.get(i));
                                        }
                                    } else {
                                        page--;
                                    }
                                } else {
                                    page--;
                                }
                                collegeIndexVolunteerListAdapter.notifyDataSetChanged();
                            }
                            break;

                        default:
                            break;

                    }
                    break;
                case Constant.ERROR_CODE_FINISH:
                    my_focus_volunteer_refreshLayout.OnRefreshComplete();
                    break;
                default:
                    break;
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.my_focus_volunteer, null);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDataFromInterNet(page, ONFRESH);
    }

    private void initView() {
        my_focus_volunteer_refreshLayout = (MyPtrRefresh_LoadView) view.findViewById(R.id.my_focus_volunteer_refreshLayout);
        my_focus_volunteer_SwipeMenuListView = (ListView) view.findViewById(R.id.my_focus_volunteer_SwipeMenuListView);
        my_focus_volunteer_refreshLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, ONLOAD);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, ONFRESH);
            }
        });
        arrayList1 = new ArrayList<>();
        collegeIndexVolunteerListAdapter = new CollegeIndexVolunteerListAdapter(getMyActivity(),arrayList1);
        my_focus_volunteer_SwipeMenuListView.setAdapter(collegeIndexVolunteerListAdapter);

        my_focus_volunteer_SwipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getMyActivity(), Volunteer.class);
                intent.putExtra("type", "vip");
                intent.putExtra("id", arrayList1.get(position).getId());
                startActivityForResult(intent, 0);
            }
        });

    }

    private void getDataFromInterNet(int page, int flag) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", "expert");
        hashMap.put("page", String.valueOf(page));
        new InterNetController(getMyActivity(), Constant.ATTENTIONINFO, handler, hashMap, flag,false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MyLogUtils.e(TAG,"+"+resultCode);
        if(data!=null) {
            boolean changeFocus = data.getBooleanExtra("changeFocus", false);
            if (changeFocus) {
                page = 1;
                getDataFromInterNet(page, ONFRESH);
            }
        }
    }
}